// pages/sessionPage/sessionPage.js
const App=getApp();
const db=wx.cloud.database();
const _ = db.command;
var   msgWatcher=null;
const myOpenid = App.globalData.openid;
const myUserInfo = App.globalData.userInfo.userInfo;//此处未赋值成功
const recorderManager = wx.getRecorderManager();//录音
const innerAudioContext = wx.createInnerAudioContext();//播放语音
var loading = true;
Page({

  data: {
    sid:'',
    recordimg:'../../images/voice.png',
    recording:false,
    voiceTip:'按住 说话',
    showtip:'上滑取消',
    cancelSendStatus: false,
    inputMode:'text',
    InputBottom: 0,
    inputContent:'',
    scrollId:'',
    modalName:null,
    systemInfo:{},
    myOpenid:myOpenid,
    myUserInfo:App.globalData.userInfo.userInfo,//此处未赋值成功
    hisUserInfo:'',
    msgList:[],
    fileImg:'https://636c-cloud1-5g9np9s88a72a0bc-1311684025.tcb.qcloud.la/chat-file.png?sign=cfc4d6ef5d729805f7691b9ce6738421&t=1653961271'
  },

  onLoad:async function (e) {
    loading=true;
    if(msgWatcher) await msgWatcher.close();
    console.log('进入会话页，携带会话号',e.session_id);
    console.log("hisUserInfo",e.hisUserInfo);
    this.data.sid = e.session_id;
    this.setData({
      hisUserInfo: e.hisUserInfo,
      myUserInfo:App.globalData.userInfo.userInfo,
    })
    var that=this;
    //获取系统信息，得到可用窗口高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          systemInfo: res,
        })
      }
    })
    //开启集合chat_msg.session_id的消息监听
    setTimeout(this.initWatcher,1000)
    //初始化录音器
    this.initRecord();
  },
  //消息监听器开启
  async initWatcher(){
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    console.log('initWatcher函数执行，消息监听器开启'); 
    msgWatcher = await db.collection('chat_msg')
    .where({
      session_id : this.data.sid,
    })
    .watch({
      onChange:this.onChange.bind(this),
      onError:function(err){
        console.log("onError",err);
        wx.showModal({
          title: '提示',
          content: '数据监听失败',
          showCancel:false,
          confirmText:'重新加载',
          success (res) {
            if (res.confirm) {
              that.onLoad();
            } 
          }
        })
      }
    })
  },
  //消息监听回调函数
  onChange(snapshot){
    console.log('onChange监听回调函数的snapshot',snapshot)
    //即时聊天
    this.sortRenderMsg(snapshot.docs[0].msg_set)
  },
  //对消息对象数组进行时间排序并渲染
  async sortRenderMsg(e){
    console.log('排序渲染函数执行');
    var list=e.sort(function(a,b){return a.createTimeStamp-b.createTimeStamp});
    await this.setData({
      msgList : list,
      scrollId: 'msg-'+(list.length-1)
    });
    if(loading==true){
      wx.hideLoading({
        complete: () => {loading=false},
      })
    }
  },
  //输入框控制
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  //显示底部窗口
  showModal(e) {
      this.setData({
        modalName:"bottomModal"
      })
  },
  //隐藏底部窗口
  hideModal(e) {
      this.setData({
        modalName: null
      })
  },
  //获取输入框内容
  onInputContent(e){
    this.data.inputContent=e.detail.value
  },
  //发送文字函数//调用了云函数lyc_consult.sendMsg
  sendContent(){
    var that=this;
    console.log("点击了发送")
    if(!this.data.inputContent){return}
    else{
      const msg={
        createTimeStamp:Date.now(),
        creator: myOpenid,
        content: this.data.inputContent,
        type:'text'
      }           
      this.setData({
          inputContent:''
      })   
      wx.cloud.callFunction({
        name:'lyc_consult',
        data:{
          type:'sendMsg',
          msg: msg,
          sid: that.data.sid,
          sendType:'text',
        }
      })
      .then((res)=>{
        console.log('云函数senMsg调用成功',res)
        if(res.result.errCode!=0){
          wx.showToast({
            title: res.result.errMsg,
            icon:'error'
          })
        }
      })
      .catch((err)=>{
        console.log('调用云函数sendMsg失败',err);
        wx.showToast({
          title: '发送失败',
          icon:'error'
        })
      })
    }
  },
  //发送图片
  sendPic(){
    var that = this;
    //选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        console.log('chooseImage调用成功',res.tempFilePaths[0]);
        var content=res.tempFilePaths[0];
        //获取全局文件管理器  读取本地文件内容
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          //成功的回调
          success: res => { 
            //文件内容
            var bufferData = res.data;
            //加载中
            wx.showLoading({
              title: '图片发送',
              mask:true
            })
            //调用云函数 传入文件内容
            wx.cloud.callFunction({
              name: 'lyc_consult',
              data: {
                type: 'sendMsg',
                sendType:'image',
                sid: that.data.sid,
                msg: {content:bufferData,}
              },
              success: res => {
                console.log('云函数sendMsg成功:图片',res);
                if(res.result.errCode!=0){
                  wx.showToast({
                    title: res.result.errMsg,
                    icon:'error'
                  })
                }
              },
              fail: res => {
                console.log(res);
                wx.showToast({
                  title: '发送失败',
                  icon:'error'
                })
              },
              complete: res => {
                wx.hideLoading();
                that.hideModal();
              }
            })
          }
        })
      }
    })
  },
  //点击图片预览
  viewImage(e){
    var urls = [e.currentTarget.dataset.url]
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: urls, // 需要预览的图片http链接列表
      showmenu : true
    })
  },
  //发送视频
  sendVideo(){
      var that = this;
      console.log("sendVideo");
      wx.chooseMedia({
        count: 1,
        mediaType: ['video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          const time = Date.now();
          console.log('视频的临时文件路径',res.tempFiles[0].tempFilePath)
          wx.cloud.uploadFile({
            cloudPath: 'chat-video/'+time+'.mp4',
            filePath: res.tempFiles[0].tempFilePath
          })
          .then((res)=>{
            wx.showLoading({
              title: '视频发送',
              mask:true
            })
            console.log('视频上传成功');
            const content = res.fileID;
            wx.cloud.callFunction({
              name: 'lyc_consult',
              data: {
                type: 'sendMsg',
                sendType:'video',
                sid: that.data.sid,
                msg: content
              },
              success: res => {
                console.log('云函数推送视频消息到会话消息数组')
              },
              fail: res => {
                console.log(res)
              },
              complete: res => {
                wx.hideLoading();
                that.hideModal();
              }
            })
  
          })
        }
      })
  },
  //发送文件
  sendFile(){
    var that = this
    wx.chooseMessageFile({
      count: 1,//能选择文件的数量
      type: 'file',//能选择文件的类型,我这里只允许上传文件
      success(res) {        
        const tempFilePaths = res.tempFiles[0].path;//文件临时路径     
        const fileName =  res.tempFiles[0].name;//文件名
        //保证文件名长度在20内
        //if(fileName.length>20){fileName = fileName.substr(0,20)+'...'}
        const houzhui = tempFilePaths.match(/\.[^.]+?$/)[0];//后缀名的获取
        const cloudpath = 'chat-file/' + Date.now() + houzhui;//存储在云存储的地址
        //获取fileID
        wx.cloud.uploadFile({
          cloudPath: cloudpath,
          filePath: tempFilePaths,
          success: res => {
            wx.showLoading({
              title: '文件发送',
              mask: true
            })
            //存储fileID，之后用的到
            const content = {fileID: res.fileID,
                             fileName: fileName};
            wx.cloud.callFunction({
              name: 'lyc_consult',
              data: {
                type: 'sendMsg',
                sendType:'file',
                sid: that.data.sid,
                msg: content
              },
              success: res => {
                console.log('云函数推送文件消息到会话消息数组')
              },
              fail: res => {
                console.log(res)
              },
              complete: res => {
                wx.hideLoading();
                that.hideModal();
              }
            })
          },
          fail: err => {
            console.log('wx.chooseMessageFile',err)
          },
        })
      }
    }) 
  },
  //打开文件
  viewFile(e){
    var fileid = e.currentTarget.dataset.fileid;//云文件fileID
    var that = this;
    wx.cloud.downloadFile({
      fileID: fileid,
    })
    .then((res)=>{
      console.log('res',res.tempFilePath);
      wx.openDocument({
        filePath: res.tempFilePath,
        showMenu: true,
      })
      .then(()=>{})
      .catch((err)=>{console.log('文件预览失败',err);})
    })
  },
  //切换输入模式
  switchInputMode(){
    this.setData({
      inputMode: this.data.inputMode==='text' ? 'voice' : 'text'
    })
  },
  //初始化录音器
  initRecord(){
    console.log('初始化录音器');
    //监听录音开始事件
    recorderManager.onStart(()=>{
      console.log('开始录音');

    });
    //监听录音结束事件
    recorderManager.onStop((res)=>{
      console.log('录音结束回调')
      if(this.data.cancelSendStatus) {
        console.log('取消录音');
        this.data.cancelSendStatus=false;
        return
      };
      let src = res.tempFilePath;  
      let duration=res.duration;
      if(duration<=1000) {
        console.log('stop,说话时间太短')
        wx.showToast({
          title: '说话时间太短',
          icon:'error'
        })
        return
      }else{duration = Math.floor(res.duration / 1000);}
      //上传云存储
      wx.cloud.uploadFile({
      cloudPath: 'chat-audio/'+Date.now()+".aac",
      filePath: src
      })
      .then((res)=>{
        console.log('录音上传云存储成功',res);
        //语音消息通过云函数上传数据库
        let obj = {fileID: res.fileID, duration: duration}
        wx.cloud.callFunction({
          name:"lyc_consult",
          data:{
            type:'sendMsg',
            sendType:'audio',
            msg: obj,
            sid: this.data.sid
          }
        })
        .then((res)=>{
          console.log('调用云函数（语音）成功')
        })
      })
      .catch((res)=>{
        console.log('失败',res);
        wx.showToast({
          title: '发送失败',
          icon:'error'
        })
      })
    })
    //错误回调
    recorderManager.onError((res) => {
        console.log('录音错误回调',res);
    })
  },
  //开始录音
  async RecordStart(e){
    console.log('手指触摸',e)
    var startY = e.touches[0].clientY; //初始Y坐
    this.data.startY = startY;   
    recorderManager.start();
    this.setData({
      recording: true,
      voiceTip: '松开 发送',
      showtip: '上滑取消',
    });
  },
  //手指移动
  RecordMove(e){
    var moveY = e.touches[0].clientY; //移动的Y坐标
    if ( this.data.startY - moveY >= 100 ) {
      this.setData({
        cancelSendStatus: true,
        showtip: '松开手指，取消发送', 
      });
    } else {
      this.setData({
        cancelSendStatus: false,
        showtip : '上滑取消' , 
      });
    }
  },  
  //松开手指录音停止
  async RecordEnd(e){
    console.log('手指松开');
    var that = this;
    setTimeout(f,100);
    function f(){
    that.setData({
      voiceTip: '按住 发送',
      recording: false
    },()=>{recorderManager.stop();});
  }
},
  //播放录音
  voicePlay(e){
    innerAudioContext.stop();
    innerAudioContext.src = '';
    console.log(e.currentTarget.dataset.voiceid)
    let id = e.currentTarget.dataset.voiceid;
    innerAudioContext.src = id;
    innerAudioContext.onPlay(()=>{console.log('语音播放')});
    innerAudioContext.onError((res)=>{
      console.log(res.errMsg) 
      console.log(res.errCode)
    })
    innerAudioContext.play();
  },
  onHide: function () {
    console.log('页面隐藏')
    
  },
  onUnload:async function (){
    var that = this;
    console.log('页面卸载，监听器关闭');
    if(msgWatcher){
      await msgWatcher.close();
      console.log('XXX',msgWatcher)
    }
    innerAudioContext.stop();
    //离开页面时，在使用者的对应会话 数组元素 中 更新 离开时间
    await wx.cloud.callFunction({
      name:'lyc_consult',
      data:{
        type:'leaveSession',
        sid: that.data.sid,
      }
    })
    .then((res)=>{
      console.log('云函数leaveSession调用成功',res)
    })
  }
})